

```{r setup, include=FALSE}
rm(list=ls())
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
library(haven)
require(memisc)
require(stringi)
library(stargazer)
library(coefplot)
library(interplot)
library(sjPlot)
library(sjmisc)
library(sjlabelled)
library(stargazer)
library(stringr)
library(tidyverse)
library(ggplot2)
library(psych)
library(margins)
library(dplyr)
library(modelsummary)
library(makedummies)
library(ggeffects)
library(estimatr)
library(ggpubr)
library(MASS)

theme_set(theme_sjplot())


data1 <- read_csv("~/Turkey_securitization2024_num.csv")
#### outcome：数字が大きいほど支持しないので逆順に ####
data2<-
  data1 %>%
  mutate(Q44R = - Q44 + 5)
data3<-
  data2 %>%
  mutate(Q45R = - Q45 + 5)
data4<-
  data3 %>%
  mutate(Q47R = - Q47 + 5)
data5<-
  data4 %>%
  mutate(Q48R = - Q48 + 5)

dat <- mutate_at(data5, c("Q44R","Q45R","Q47R","Q48R"), ~replace(., is.na(.),0))
dat$attitude <- (dat$Q48R + dat$Q47R + dat$Q44R + dat$Q45R)

dat$picture_d <- ifelse(dat$Q44R>=1, 1,0)
dat$securitization_d <- ifelse(dat$Q45R>=1,1,0)
dat$pic_sec_d <- ifelse(dat$Q47R>=1,1,0)
dat$control_d <- ifelse(dat$Q48R>=1,1,0)
dat$syrian_neg <- ifelse(dat$Q29==1,1,0)
dat$AKP <- ifelse(dat$Q16==1,1,0)
dat$MHP <- ifelse(dat$Q16==3,1,0)
dat$CHP <- ifelse(dat$Q16==4,1,0)
dat$Erdogan <- ifelse(dat$Q17==1,1,0)
dat$sex <- dat$Q2
dat$age <- dat$Q3
dat$education <- dat$Q5

```

## Regression baseline

```{r}
reg01 <- lm(formula = attitude ~ picture_d + securitization_d + pic_sec_d + sex + age + education,
            data=dat)
summary(reg01)

dat <- dat %>%
  mutate(dep=as.factor(attitude))

ologit01 <- polr(formula = dep ~ picture_d + securitization_d + pic_sec_d + sex + age + education,
            data=dat)
summary(ologit01)

```

## Regresson Erdogan

```{r}
reg02 <- lm(formula = attitude ~ picture_d + securitization_d + pic_sec_d + Erdogan + sex + age + education,
            data=dat)
summary(reg02)

ologit02 <- polr(formula = dep ~ picture_d + securitization_d + pic_sec_d + Erdogan + sex + age + education,
            data=dat)
summary(ologit02)

```
```{r}
reg03 <- lm(formula = attitude ~ picture_d + securitization_d + pic_sec_d +  AKP + MHP  + sex + age + education,
            data=dat)
summary(reg03)

ologit03 <- polr(formula = dep ~ picture_d + securitization_d + pic_sec_d + AKP + MHP + sex + age + education,
            data=dat)
summary(ologit03)

```


```{r}
reg04 <- lm(formula = attitude ~ picture_d + securitization_d + pic_sec_d + syrian_neg + sex + age + education,
            data=dat)
summary(reg04)

ologit04 <- polr(formula = dep ~ picture_d + securitization_d + pic_sec_d + syrian_neg + sex + age + education,
            data=dat)
summary(ologit04)
```


```{r}
reg05 <- lm(formula = attitude ~ picture_d + securitization_d + pic_sec_d + Erdogan + AKP + MHP + syrian_neg + syrian_neg:Erdogan + sex + age + education,
            data=dat)
summary(reg05)

ologit05 <- polr(formula = dep ~ picture_d + securitization_d + pic_sec_d + Erdogan + AKP + MHP + syrian_neg + syrian_neg:Erdogan + sex + age + education,
            data=dat)
summary(ologit05)

```
### Regression Table
```{r}
reg_table <- mtable("Baseline"=reg01, "Erdogan"=reg02,
                     "Parties"=reg03, "Negative to Syrians"=reg04, "Full Model"=reg05,
                     summary.stats=c("adj. R-squared","F","p","N"))
print(reg_table)
```

### Ordered logit Table
```{r}
library(modelsummary)
ologit_table <- list(ologit01,ologit02,ologit03,ologit04,ologit05)
names(ologit_table)<-c("Baseline","Erdogan","Parties","Negative opinion about Syrians","Full Model")
msummary(ologit_table,stars=TRUE,gof_omit='RMSE|AIC|BIC|Log.Lik.')
```


```{r}
multiplot(reg01,reg02,reg03,reg04,reg05,
          title="OLS results",
          names=c(reg01="1.Baseline",reg02="2.Erdogan",reg03="3.Parties",reg04="4.Negative opinion about Syrians", reg05="5.Full Model"),
          sort = c("magnitude"),
          intercept=FALSE,
          plot.shapes=FALSE,
          coefficients = c('picture_d',
                           'securitization_d',
                           'pic_sec_d',
                           'Erdogan',
                           'AKP',
                           'MHP',
                          'syrian_neg'),
           newNames=c('picture_d'="Erdogan picture",
                      'securitization_d'="Securitization speech",
                      'pic_sec_d'="Picture & Speech",
                      'Erdogan'="Vote for Erdogan",
                      'AKP'="Vote for AKP",
                      'MHP'="Vote for MHP",
                      'syrian_neg'="Negative to Syrians"),
           single = FALSE,
           ncol = 3) +
   theme_bw() +
   theme(legend.position = "none")+
   ggtitle("")
```

```{r}
temp <- select(dat,attitude, picture_d, securitization_d, pic_sec_d, control_d, syrian_neg, AKP, MHP, CHP, Erdogan, sex, age, education)
head(temp)
library(psych)
describeBy(temp)
```
